package com.zzyl.member.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.member.config.WechatConfigProperties;
import com.zzyl.member.service.WechatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class WechatServiceImpl implements WechatService {
    private final WechatConfigProperties wechatConfigProperties;

    // 登录
    private final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";
    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Override
    public String getOpenid(String code) {
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("appid",wechatConfigProperties.getAppId());
            paramMap.put("secret",wechatConfigProperties.getAppSecret());
            paramMap.put("js_code",code);
//            String res = HttpUtil.get(REQUEST_URL, paramMap);
//            JSONObject jsonObject = JSONUtil.parseObj(res);
//        if(jsonObject.containsKey("errcode")) {
//            throw new RuntimeException(jsonObject.getStr("errmsg"));
//        }
//            String openid = jsonObject.getStr("openid");
        String res = HttpUtil.get(REQUEST_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(res);
        if(jsonObject.containsKey("errcode"))
            throw new RuntimeException("获取失败");

        return jsonObject.getStr("openid");
    }

    @Override
    public String getPhone(String detailCode) {
        //方法1
//        String token = token();
//
//        Map<String, Object> paramMap = MapUtil.<String, Object>builder().put("code", detailCode).build();
//        String res = HttpUtil.post(PHONE_REQUEST_URL + token, JSONUtil.toJsonStr(paramMap));
//        System.out.println("res = " + res);
//        JSONObject jsonObject = JSONUtil.parseObj(res);
//        if(jsonObject.getInt("errcode")!=0)
//            throw new RuntimeException(jsonObject.getStr("errmsg"));
//        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
        String token = token();
        String url = PHONE_REQUEST_URL + token;
        Map<String, Object> paramMap = MapUtil.<String, Object>builder().put("code", detailCode).build();
        String res = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        JSONObject jsonObject = JSONUtil.parseObj(res);
        if(jsonObject.getInt("errcode") != 0)
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        return  jsonObject.getJSONObject("phone_info").getStr("phoneNumber");

    }
    public String token(){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid",wechatConfigProperties.getAppId());
        paramMap.put("secret",wechatConfigProperties.getAppSecret());
        String res = HttpUtil.get(TOKEN_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(res);
        if(ObjectUtil.isNotEmpty(jsonObject.getInt("errcode")))
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        String accessToken = jsonObject.getStr("access_token");
        return accessToken;
    }
}
